package View;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionListener;

import Model.*;

/**
 * Window is the main window of the application
 * @author Johanna
 *
 */
public class Window extends JFrame implements WindowListener {
	
	private static final long serialVersionUID = 1L;

	private FormPane formPane;
	private InfoContactPane infoPane;
	private ListContactsPane listPane;
	private JPanel rightPane;
	
	public static final String WINDOW_TITLE = "Address Book";

	private ContactsList model;

	public static final String FIRSTNAME = "First name:";
	public static final String CREATE = "Create";

	/** 
	 * Constructor 
	 */
	public Window(ContactsList model)
	{
		this.model = model;
		setTitle(WINDOW_TITLE);

		this.formPane = new FormPane();
		this.infoPane = new InfoContactPane();
		this.listPane = new ListContactsPane();

		this.formPane.setBorder(BorderFactory.createRaisedBevelBorder());
		this.infoPane.setBorder(BorderFactory.createRaisedBevelBorder());
		this.listPane.setBorder(BorderFactory.createRaisedBevelBorder());

		// Design the right panel of the app
		this.rightPane = new JPanel(new BorderLayout());

		this.rightPane.add(this.infoPane, BorderLayout.NORTH);
		this.rightPane.add(this.formPane, BorderLayout.SOUTH);

		// Organize the layout of the frame
		Container contentPane = getContentPane();
		((JComponent) contentPane).setBorder(new EmptyBorder(15, 15, 15, 15));
		this.setSize(900,700); 
		setLayout(new BorderLayout(5,5));
		add(this.listPane, BorderLayout.WEST);
		add(this.rightPane, BorderLayout.EAST);
		add(new JLabel("ADDRESS BOOK"), BorderLayout.NORTH);

		this.addWindowListener(this);
	}

	/**
	 * Method to add listeners on buttons
	 * @param buttonListener
	 */
	public void addButtonListener(ActionListener buttonListener) {
		// TODO Auto-generated method stub
		this.formPane.getCreateButton().addActionListener(buttonListener); 
	}

	public void reset() {
		this.formPane.reset();
	}

	public void addListSelectedListener(ListSelectionListener listListener) {
		this.listPane.getListing().addListSelectionListener(listListener);
	}

	public void addDeleteListener(ActionListener deleteListener) {
		this.infoPane.getDeleteButton().addActionListener(deleteListener);
	}

	public void addSortLetterListener(ActionListener sortLetterListener) {
		this.listPane.getLetterButton().addActionListener(sortLetterListener);
	}

	public void addSortGroupListener(ActionListener sortGroupListener) {
		this.listPane.getGroupButton().addActionListener(sortGroupListener);
	}

	public void addSortAlphaListener(ActionListener sortAlphaListener) {
		this.listPane.getAlphaOrderButton().addActionListener(sortAlphaListener);
	}

	public void addSendEmailListener(ActionListener sendEmailListener) {
		this.infoPane.getSendMailButton().addActionListener(sendEmailListener);
	}

	public void addSeeHomepageListener(ActionListener seeHomepageListener) {
		this.infoPane.getHomepageButton().addActionListener(seeHomepageListener);
	}

	public void addOpenModifyListener(ActionListener openModifyListener) {
		this.infoPane.getModifyButton().addActionListener(openModifyListener);
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub

		//When closing the window we save the list in a csv file
		File csvFile = new File("adressBook.csv"); 
		if (!csvFile.exists())
			try {
				throw new FileNotFoundException("The file doesn't exist");
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		else{
			PrintStream l_out;
			try {
				l_out = new PrintStream(new FileOutputStream("adressBook.csv"));

				for (int i=0; i< this.model.getListing().size(); i++)
				{
					l_out.print("\n");
					l_out.print(model.getListing().get(i).toString());	
				} 

				l_out.flush(); 
				l_out.close();

				dispose();
			} 

			catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		// The CSV file is loaded while opening the window
		try {
			this.model.readCsv();
			this.listPane.getListing().setModel(model.createDefaultList());
		} 

		catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	public FormPane getFormPane() {
		return this.formPane;
	}

	public InfoContactPane getInfoPane() {
		return this.infoPane;
	}

	public ListContactsPane getListPane() {
		return this.listPane;
	}

	public JPanel getRightPane() {
		return this.rightPane;
	}

}
